const { killExcel, writeABook } = require('../index.js')
const Fs = require('fs-extra')
let path = ''
/**
 * 用于向主进程发送数据
 */
function postMsg() {
  process.send(...arguments)
}

/**
 * @param root0
 * @param root0.path
 * @param root0.json
 * @param root0.id
 */
function handleData({ path: filePath, json, id }) {
  filePath && (path = filePath)
  try {
    Fs.removeSync(path)
  } catch (error) {
    killExcel()
  }
  if (json) {
    writeABook(path, json)
  }
  postMsg({ path: filePath, complete: true, id })
}

/**
 * 用于接收主进程发来的数据
 */
process.on('message', handleData)
